<?php

namespace wicked\core;

abstract class Event
{

    protected static $events = [];

    public static function fire($event, $args = [])
    {
        if($event != 'event.fire')
            static::fire('event.fire', compact('event', 'args'));

        $event = (string)$event;
        if(array_key_exists($event, self::$events))
            foreach(self::$events[$event] as $e)
                call_user_func_array($e, $args);
    }

    public static function listen($event, $callback)
    {
        $event = (string)$event;
        if(!array_key_exists($event, self::$events))
            self::$events[$event] = [];

        self::$events[$event][] = $callback;
    }

    public static function has($event)
    {
        return !empty(self::$events[(string)$event]);
    }

    public static function forget($event)
    {
        static::$events[$event] = [];
    }

    public static function count($event)
    {
        return static::has($event) ? count(self::$events[(string)$event]) : 0;
    }

}
